<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Data_rekap extends CI_Controller
{
	function __construct(){
		parent::__construct();	
		$this->load->library('form_validation');			
		$this->load->model('ipkd_model');
		$this->load->model('dpb_model');
		$this->load->model('dibsa_model');		
	}
	
	function index()
	{		
		$this->data();
	}
	
	function menu_rekap()
	{
		$kecamatan = $this->ipkd_model->get_all_kecamatan();
		
		$kecamatan_options['all'] = '-- Pilih Semua Kecamatan --';
					foreach($kecamatan->result() as $kcm){
					$kecamatan_options[$kcm->NAMA_KECAMATAN] = $kcm->NAMA_KECAMATAN;
					}

		$program_options['all']= '-- Pilih Dokumen --';
		$program_options['ipkd']= 'Data Ijin Pemakaian Kekayaan Daerah';
		$program_options['dpb']= 'Data Peil Banjir';
		$program_options['dibsa']= 'Data Batas Sempadan Air';

		$status_options['all']= '-- Pilih Status Pembayaran --';
		$status_options['0']= 'Belum Membayar';
		$status_options['1']= 'Sudah Membayar';

		$bulan_options['all'] = '-- Pilih Semua Bulan --';
		$bulan_options['1'] = 'Januari';
		$bulan_options['2'] = 'Februari';
		$bulan_options['3'] = 'Maret';
		$bulan_options['4'] = 'April';
		$bulan_options['5'] = 'Mei';
		$bulan_options['6'] = 'Juni';
		$bulan_options['7'] = 'Juli';
		$bulan_options['8'] = 'Agustus';
		$bulan_options['9'] = 'September';
		$bulan_options['10'] = 'Oktober';
		$bulan_options['11'] = 'November';
		$bulan_options['12'] = 'Desember';
		
		$data['program_options'] = $program_options;
		$data['kecamatan_options'] = $kecamatan_options;
		$data['status_options'] = $status_options;

		$data['bulan_options'] = $bulan_options;
		
		$data['content'] = $this->load->view('rekap/menu_rekap',$data,true);
		$this->load->view('main',$data);
	
	}
	
	function data()
	{	
		$program = $this->input->post('program');
		$tahun = $this->input->post('tahun');
		$kecamatan = $this->input->post('kecamatan');
		$bulan1 = $this->input->post('bulan1');
		$bulan2 = $this->input->post('bulan2');
		$status = $this->input->post('status_bayar');
		
		if ($this->cek_validasi() == FALSE){
			$kecamatan = $this->ipkd_model->get_all_kecamatan();
		
			$kecamatan_options['all'] = '-- Pilih Semua Kecamatan --';
						foreach($kecamatan->result() as $kcm){
						$kecamatan_options[$kcm->NAMA_KECAMATAN] = $kcm->NAMA_KECAMATAN;
						}
			$program_options['all']= '-- Pilih Dokumen --';
			$program_options['ipkd']= 'Data Ijin Pemakaian Kekayaan Daerah';
			$program_options['dpb']= 'Data Peil Banjir';
			$program_options['dibsa']= 'Data Batas Sempadan Air';

			$status_options['all']= '-- Pilih Status Pembayaran --';
			$status_options['0']= 'Belum Membayar';
			$status_options['1']= 'Sudah Membayar';

			$bulan_options['all'] = '-- Pilih Semua Bulan --';
			$bulan_options['1'] = 'Januari';
			$bulan_options['2'] = 'Februari';
			$bulan_options['3'] = 'Maret';
			$bulan_options['4'] = 'April';
			$bulan_options['5'] = 'Mei';
			$bulan_options['6'] = 'Juni';
			$bulan_options['7'] = 'Juli';
			$bulan_options['8'] = 'Agustus';
			$bulan_options['9'] = 'September';
			$bulan_options['10'] = 'Oktober';
			$bulan_options['11'] = 'November';
			$bulan_options['12'] = 'Desember';
			
			$data['program_options'] = $program_options;
			$data['kecamatan_options'] = $kecamatan_options;
			$data['status_options'] = $status_options;

			$data['bulan_options'] = $bulan_options;
			
			$data['content'] = $this->load->view('rekap/menu_rekap',$data,true);
			$this->load->view('main',$data);
		}
		else
		{

		// if($program == "all")
		// {
		// 	if($kecamatan == "all")
		// 	{
		// 	$program_ipkd = $this->ipkd_model->get_all_ipkd_thn($tahun);
		// 	$program_dibsa = $this->ipkd_model->get_all_dibsa_thn($tahun);
		// 	$program_dpb = $this->ipkd_model->get_all_dpb_thn($tahun);
			
		// 	$data['program_ipkd'] = $program_ipkd;
		// 	$data['program_dpb'] = $program_dpb;
		// 	$data['program_dibsa'] = $program_dibsa;
			
		// 	$data['content'] = $this->load->view('rekap_result',$data,true);
		// 	$this->load->view('main',$data);
		// 	}
		// 	else
		// 	{
		// 	$program_ipkd = $this->ipkd_model->get_all_ipkd_cari($tahun,$kecamatan);
		// 	$program_dibsa = $this->ipkd_model->get_all_dibsa_cari($tahun,$kecamatan);
		// 	$program_dpb = $this->ipkd_model->get_all_dpb_cari($tahun,$kecamatan);
			
		// 	$data['program_ipkd'] = $program_ipkd;
		// 	$data['program_dpb'] = $program_dpb;
		// 	$data['program_dibsa'] = $program_dibsa;
			
		// 	$data['content'] = $this->load->view('rekap_result',$data,true);
		// 	$this->load->view('main',$data);
		// 	}
		// }
		if($program == "ipkd")
		{
			if($bulan1 != "all" && $bulan2 != "all" && $kecamatan == "all"){
				$program_ipkd = $this->ipkd_model->get_ipkd_bulan($bulan1,$bulan2,$tahun);
				$retribusi = $this->ipkd_model->get_sum_ipkd_bulan($bulan1,$bulan2,$tahun);

				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}

				$data['ipkd'] = $program_ipkd;
				$data['bulan1'] = $this->_baca_bulan($bulan1);
				$data['bulan2'] = $this->_baca_bulan($bulan2);
				$data['tahun'] = $tahun;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));

				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$this->_baca_bulan($bulan1)."_".$this->_baca_bulan($bulan2)."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_bulan',$data);
			}
			else if($bulan1 != "all" && $bulan2 != "all" && $kecamatan != "all"){
				$program_ipkd = $this->ipkd_model->get_ipkd_bulan_kecamatan($bulan1,$bulan2,$tahun,$kecamatan);
				$retribusi = $this->ipkd_model->get_sum_ipkd_bulan_kecamatan($bulan1,$bulan2,$tahun,$kecamatan);

				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}

				$data['ipkd'] = $program_ipkd;
				$data['bulan1'] = $this->_baca_bulan($bulan1);
				$data['bulan2'] = $this->_baca_bulan($bulan2);
				$data['kecamatan'] = $kecamatan;
				$data['tahun'] = $tahun;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));

				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$kecamatan."_".$this->_baca_bulan($bulan1)."_".$this->_baca_bulan($bulan2)."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_bulan_kecamatan',$data);
			}
			else if($bulan1 == "all" && $bulan2 == "all" && $kecamatan == "all" && $status == "all"){
				$program_ipkd = $this->ipkd_model->get_ipkd_tahun($tahun);
				$retribusi = $this->ipkd_model->get_sum_ipkd_tahun($tahun);
				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}
				$data['ipkd'] = $program_ipkd;
				$data['tahun'] = $tahun;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_tahun',$data);
			}
			else if($bulan1 != "all" && $bulan2 != "all" && $kecamatan == "all" && $status != "all"){
				$program_ipkd = $this->ipkd_model->get_ipkd_bulan_bayar($bulan1,$bulan2,$tahun,$status);
				$retribusi = $this->ipkd_model->get_sum_ipkd_bulan_bayar($bulan1,$bulan2,$tahun,$status);

				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}

				$data['ipkd'] = $program_ipkd;
				$data['bulan1'] = $this->_baca_bulan($bulan1);
				$data['bulan2'] = $this->_baca_bulan($bulan2);
				$data['tahun'] = $tahun;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));

				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$this->_baca_bulan($bulan1)."_".$this->_baca_bulan($bulan2)."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_bulan',$data);
			}
			else if($bulan1 != "all" && $bulan2 != "all" && $kecamatan != "all" && $status != "all"){
				$program_ipkd = $this->ipkd_model->get_ipkd_bulan_kecamatan_bayar($bulan1,$bulan2,$tahun,$kecamatan,$status);
				$retribusi = $this->ipkd_model->get_sum_ipkd_bulan_kecamatan_bayar($bulan1,$bulan2,$tahun,$kecamatan,$status);

				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}

				$data['ipkd'] = $program_ipkd;
				$data['bulan1'] = $this->_baca_bulan($bulan1);
				$data['bulan2'] = $this->_baca_bulan($bulan2);
				$data['kecamatan'] = $kecamatan;
				$data['tahun'] = $tahun;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));

				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$kecamatan."_".$this->_baca_bulan($bulan1)."_".$this->_baca_bulan($bulan2)."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_bulan_kecamatan',$data);
			}
			else if($bulan1 == "all" && $bulan2 == "all" && $kecamatan == "all" && $status != "all"){
				$program_ipkd = $this->ipkd_model->get_ipkd_tahun_bayar($tahun,$status);
				$retribusi = $this->ipkd_model->get_sum_ipkd_tahun_bayar($tahun,$status);
				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}
				$data['ipkd'] = $program_ipkd;
				$data['tahun'] = $tahun;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));
				
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_tahun',$data);
			}
			else{
				$program_ipkd = $this->ipkd_model->get_ipkd_tahun_kecamatan($tahun,$kecamatan);
				$retribusi = $this->ipkd_model->get_sum_ipkd_tahun_kecamatan($tahun,$kecamatan);
				foreach ($retribusi->result() as $row){
                	$jumlah = $row->JUMLAH_RETRIBUSI;
            	}
				$data['ipkd'] = $program_ipkd;
				$data['tahun'] = $tahun;
				$data['kecamatan'] = $kecamatan;
				$data['jumlah'] = $jumlah;
				$data['terbilang'] = ucfirst($this->_baca($jumlah));
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_ipkd_".$kecamatan."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_ipkd_tahun_kecamatan',$data);
			}
		}
		else if($program == "dpb")
		{
			if($kecamatan == "all"){
				$program_dpb = $this->ipkd_model->get_all_dpb_thn($tahun);
				$data['dpb'] = $program_dpb;
				$data['tahun'] = $tahun;
				
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_dpb_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_dpb_tahun',$data);
			}
			else{
				$program_dpb = $this->ipkd_model->get_all_dpb_cari($tahun,$kecamatan);
				$data['dpb'] = $program_dpb;
				$data['tahun'] = $tahun;
				$data['kecamatan'] = $kecamatan;
				
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_dpb_".$kecamatan."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_dpb_tahun_kecamatan',$data);
			}
		}
		else if($program == "dibsa")
		{
			if($kecamatan == "all"){
				$program_dibsa = $this->ipkd_model->get_all_dibsa_thn($tahun);
				$data['dibsa'] = $program_dibsa;
				$data['tahun'] = $tahun;
				
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_dibsa_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_dibsa_tahun',$data);
			}
			else{
				$program_dibsa = $this->ipkd_model->get_all_dibsa_cari($tahun,$kecamatan);
				$data['dibsa'] = $program_dibsa;
				$data['tahun'] = $tahun;
				$data['kecamatan'] = $kecamatan;
				
				$header = '';
				header("Content-type: application/x-msdownload"); 
		        header("Content-Disposition: attachment; filename=Rekap_dpb_".$kecamatan."_".$tahun.".xls"); 
		        header("Pragma: no-cache"); 
		        header("Expires: 0");
		        echo $header;
				
				$this->load->view('rekap/cetak_dibsa_tahun_kecamatan',$data);
			}
		}
		
		
		}
	}

	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('tahun', 'Tahun', 'required|callback_cek_dropdown');
			$this->form_validation->set_rules('program', 'Program', 'required|callback_cek_dropdown');
			
			// Setting Error Message
			$this->form_validation->set_message('required', '%s harus diisi!');

			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
			
	}

	function cek_dropdown($value){
	$tahun= $this->input->post('tahun');
	$program= $this->input->post('program');

		if($tahun == "all"){
			$this->form_validation->set_message('cek_dropdown', 'Kolom Tahun wajib dipilih!!');
			return FALSE;
		}
		else if($tahun != "all" && $program == "all"){
			$this->form_validation->set_message('cek_dropdown', 'Kolom Dokumen wajib dipilih!!');
			return FALSE;
		}		
		else{
			return TRUE;
		}
	}

	function _baca($n) {
		$str='';
		$this->dasar = array(1 => 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam','tujuh', 'delapan', 'sembilan');
		$this->angka = array(1000000000, 1000000, 1000, 100, 10, 1);
		$this->satuan = array('milyar', 'juta', 'ribu', 'ratus', 'puluh', '');

		$i = 0;
			if($n==0){
			   $str = "nol";
			}
			else{
			  while ($n != 0) {
				  $count = (int)($n/$this->angka[$i]);
			  if ($count >= 10) {
				  $str .= $this->_baca($count). " ".$this->satuan[$i]." ";
			  }else if($count > 0 && $count < 10){
				  $str .= $this->dasar[$count] . " ".$this->satuan[$i]." ";
			  }
			  $n -= $this->angka[$i] * $count;
			  $i++;
			   }
			   $str = preg_replace("/satu puluh (\w+)/i", "\\1 belas", $str);
			   $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i", "se\\1", $str);
			}
		return $str;
	}
	
	function _format_tanggal($tanggal=1,$bulan=1,$tahun=0,$separator=' ')
	{
		$nama_bulan = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
		return $tanggal.$separator.$nama_bulan[$bulan].$separator.$tahun;
	}
	
	function _format_bulan($bulan=1,$tahun=0,$separator=' ')
	{
		$nama_bulan = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
		return $nama_bulan[$bulan].$separator.$tahun;
	}

	function _baca_bulan($bulan)
	{
		$nama_bulan = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
		return $nama_bulan[$bulan];
	}

}//end of class